/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jaspec;

import static junit.framework.Assert.*;

/**
 * Expectation of code block result.
 */
public class Expect {

  private final Code code;

  public Expect(Code code) {
    assertNotNull("The given code is null", code);
    this.code = code;
  }

  /**
   * Expect the code to throw an exception.
   */
  public void to_throw_exception() {
    try {
      code.exec();
    } catch (Exception e) {
      return;
    }
    fail("The code didn't throw any exception");
  }

  /**
   * Expect the code to throw an exception.
   * 
   * @param exception Expected exception
   */
  public void to_throw(Class<? extends Throwable> exception) {
    try {
      code.exec();
    } catch (Exception e) {
      if (e.getClass().equals(exception)) {
        return;
      }
    }
    fail("The code didn't throw an " + exception.getCanonicalName() + " exception");
  }
}
